package com.iris.live.services.report.sources;

import com.iris.live.services.common.NumberUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.SalesRepository;
import com.iris.live.services.models.RequestBase;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import com.iris.live.services.services.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component("ExportOrderSource")
public class ExportOrderSource implements IExcelReportSource {
    @Autowired
    private SalesRepository repository;

    @Autowired
    private SalesService salesService;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) { // 表单
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商 销售行政订单导出模板", "经销商 销售行政订单导出模板", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String dealer = (String) context.get("dealer");
        Map params = (Map) context.get("param");
        String[] columns = (String[]) context.get("columns"); // 用"|"分割后的标题列
        String[] columnPair; // 标题列，以对值形式出现
        String[] keys = new String[columns.length];
        String[] titles = new String[columns.length];
        int index;
        List<Row> rows = new ArrayList<>();
//        for(index = 0; index < 5; index++) { // 空行处理
//            rows.add(new Row());
//        }
        //标题输出
        for (index = 0; index < columns.length; index++) {
            columnPair = columns[index].split(",");
            keys[index] = columnPair[1]; // map中的key
            titles[index] = columnPair[0]; // excel中的标题
        }
        rows.add(new Row(getTitleCells(titles)));
        //数据输出
        index = 0;
        RequestBase request = (RequestBase) context.get("request");
        List list = repository.getOrdersByAllOriginId(dealer, params, request); // 核心方法，数据来源
        salesService.calculateGp(list);
        if (list != null) {
            for (Object o : list) {
                rows.add(new Row(getDataCells(keys, o)));
            }
        }
        return rows;
    }

    /**
     * 获取Excel 标题行
     */
    private Cell[] getTitleCells(String[] titles) {
        Cell[] cells = new Cell[titles.length];
        for (int index = 0; index < titles.length; index++) {
            cells[index] = new Cell(titles[index], index);
        }
        return cells;
    }

    /**
     * 获取数据行
     */
    private Cell[] getDataCells(String[] columns, Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[columns.length];
//        cells[0] = new Cell(rowIndex + "", 0); // 不需要的行数处理
        Object value;
        for (int index = 0; index < columns.length; index++) {
            value = map.get(columns[index]);
            //获取数据类型，用以作为写入excel时的数据类型（修改原因：数据写入excel时，生产号以科学计数法的形式显示）
            String type = null;
            if (!StringUtils.isNullOrBlank(map.get(columns[index]))) {
                type = map.get(columns[index]).getClass().getSimpleName();
            }
            if (StringUtils.isNullOrBlank(value)) {
                cells[index] = new Cell("", index); // 自定义方法，写入某行index列
            } else if (NumberUtils.isDouble(map.get(columns[index])) && ("Double".equals(type) || "Decimal".equals(type))) { //Double型小数点后保留两位, Added by Tony, 2017-06-27
                cells[index] = new Cell(NumberUtils.format(Double.valueOf(map.get(columns[index]).toString()), 2), index);
            } else {
                cells[index] = new Cell(map.get(columns[index]) == null ? "" : map.get(columns[index]).toString(), index);
            }
        }
        return cells;
    }
}